# -*- coding: utf-8 -*-
# @author: 龙非池
from __future__ import unicode_literals

from django.db import models

from wlmodels.codes.dicts import cb16_dict, cb17_dict, cb18_dict, cb19_dict, cb30_dict, cb31_dict, cb32_dict, cd04_dict, \
    ca26_dict, ex01_dict, ex06_dict, cd22_dict, cd20_dict, ex10_dict, cd21_dict, cd24_dict, cd25_dict, cd60_dict, cd61_dict
from wlmodels.codes.fields import CrossForeignCharField
from wlmodels.codes.models import ELBaseModel, ELCA02, ELCA25, EntIdable, ELCA04, ELCA16, ELCA11


# 企业管理人员信息表
class ELManager(ELBaseModel):
    entobj = models.ForeignKey('company.ELPost', verbose_name='所属企业', db_column='entid',
                               related_name='c_manager', to_field='entid', null=True)
    personid = models.ForeignKey('company.ELPerson', verbose_name='关联个人', db_column='personid', null=True, blank=True,
                                 related_name='p_manager', to_field='personid')
    NAME = models.CharField('姓名', max_length=200, null=True, blank=True)
    POSITION = models.CharField('职务', max_length=5, null=True, blank=True, choices=cb18_dict)
    LEREPSIGN = models.NullBooleanField('是否法人', blank=True, db_index=True)
    CERNO = models.CharField('证件号码', max_length=30, null=True, blank=True, db_index=True)
    CERTYPE = models.CharField('证件类型', max_length=2, null=True, blank=True, choices=cb16_dict)
    SEX = models.CharField('性别', max_length=2, null=True, blank=True, choices=cb17_dict)
    NATDATE = models.DateField('出生日期', null=True, blank=True)
    DOM = models.CharField('住址', max_length=100, null=True, blank=True)
    POSTALCODE = models.CharField('邮编', max_length=6, null=True, blank=True)
    TEL = models.CharField('电话', max_length=30, null=True, blank=True)
    LITDEG = models.CharField('文化程度', max_length=3, null=True, blank=True, choices=cb31_dict)
    NATION = models.CharField('民族', max_length=3, null=True, blank=True, choices=cb32_dict)
    POLSTAND = models.CharField('政治面貌', max_length=3, null=True, blank=True, choices=cb30_dict)
    OCCST = models.CharField('职业状况', max_length=128, null=True, blank=True, help_text='同时对应个人独资企业投资人信息的，申请前职业状况OccStBeApp')
    OFFSIGN = models.CharField('公务员标志', max_length=2, null=True, blank=True, choices=ex06_dict)
    ACCDSIDE = models.CharField('委派方', max_length=200, null=True, blank=True, help_text='GS 3－2005标准，3.2.1.2.3　外商投资企业主要人员信息')
    CHARACTER = models.CharField('经营者主体特征', max_length=3, null=True, blank=True, choices=ex01_dict)
    COUNTRY_id = CrossForeignCharField('国别(地区)', max_length=3, null=True, blank=True, db_column='COUNTRY')
    ARRCHDATE = models.DateField('来华日期', null=True, blank=True)
    CERLSSDATE = models.DateField('签证签发日期', null=True, blank=True)
    CERVALPER = models.DateField('签证有效期', null=True, blank=True)
    CHIOFTHEDELSIGN = models.CharField('首席代表标志', max_length=2, null=True, blank=True, choices=ex06_dict)
    NOTORG = models.CharField('身份核证文件核(公)证机构(人)', max_length=100, null=True, blank=True)
    NOTDOCNO = models.CharField('身份核证文件编号', max_length=50, null=True, blank=True)
    OFFHFROM = models.DateField('任职起始日期', null=True, blank=True)
    OFFHTO = models.DateField('任职截止日期', null=True, blank=True)
    POSBRFORM = models.CharField('职务产生方式', max_length=3, null=True, blank=True, choices=cb19_dict)
    APPOUNIT = models.CharField('任命单位', max_length=100, null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '企业管理人员'
        verbose_name_plural = '企业管理人员'
        app_label = "company"
        db_table = 'company_manager'

    def _entid(self):
        from wlmodels.company.models import ELPost
        entid = ELPost.objects.filter(entid=self.entobj_id).first()
        if entid:
            return entid
        return ""

    _entid.short_description = "所属企业"

    entid = property(_entid)

    def __unicode__(self):
        return u'%s的管理人员' % self.entid

    def _country(self):
        the_country = list(ELCA02.objects.filter(code=self.COUNTRY_id).values_list("name", flat=True))
        if len(the_country):
            return the_country[0]
        return ""

    _country.short_description = "国别(地区)"

    COUNTRY = property(_country)

    @property
    def birthday(self):
        try:
            if self.NATDATE:
                return self.NATDATE.strftime("%Y-%m-%d")
            elif self.CERNO and len(str(self.CERNO)) == 18:
                return self.CERNO[6:10] + "-" + self.CERNO[10:12] + "-" + self.CERNO[12:14]
            elif self.CERNO and len(str(self.CERNO)) == 15:
                return "19" + self.CERNO[6:8] + "-" + self.CERNO[8:10] + "-" + self.CERNO[10:12]
            return ""
        except Exception:
            return ""


# 企业工商注册变更
class ELModify(ELBaseModel):
    entobj = models.ForeignKey('company.ELPost', verbose_name='所属企业', db_column='entid',
                               related_name='c_modify', to_field='entid', null=True)
    ALTITEM = models.CharField('变更项目', max_length=3, null=True, blank=True, choices=cd04_dict)
    ALTBE = models.TextField('变更前', null=True, blank=True)
    ALTAF = models.TextField('变更后', null=True, blank=True)
    ALTDATE = models.DateField('变更日期', null=True, blank=True)
    ALTTIME = models.IntegerField('变更次数', null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '企业注册变更'
        verbose_name_plural = '企业注册变更'
        app_label = "company"
        db_table = 'company_modify'

    def _entid(self):
        from wlmodels.company.models import ELPost
        entid = ELPost.objects.filter(entid=self.entobj_id).first()
        if entid:
            return entid
        return ""

    _entid.short_description = "所属企业"

    entid = property(_entid)

    def __unicode__(self):
        return u'%s的注册变更' % self.entid


# 许可证信息
class ELLicence(EntIdable):
    LIC_ID = models.CharField('许可文件ID', max_length=50)
    LICNO = models.CharField('许可文件编号', max_length=200, null=True, blank=True)
    LICNAME_id = CrossForeignCharField('许可文件', null=True, blank=True, max_length=6, db_column='LICNAME')
    VALFORM = models.DateField('有效期自', null=True, blank=True)
    VALTO = models.DateField('有效期至', null=True, blank=True)
    LICANTH = models.CharField('许可机关', max_length=200, null=True, blank=True)
    LICSTATE = models.CharField('许可文件状态', max_length=2, null=True, blank=True, choices=ca26_dict)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '许可信息'
        verbose_name_plural = '许可信息'
        app_label = "companyplus"
        db_table = 'company_certificate'

    def __unicode__(self):
        return u'%s的许可信息' % self.entid

    def _licname(self):
        the_licname = list(ELCA25.objects.filter(code=self.LICNAME_id).values_list("name", flat=True))
        if len(the_licname):
            return the_licname[0]
        return ""

    _licname.short_description = "许可证书"

    LICNAME = property(_licname)


##############################################
################### 年检信息 ##################
##############################################

# 年检基本信息
class ELAnnualCheck(EntIdable):
    ANCHEYEAR = models.CharField('年检年度', max_length=4, null=True, blank=True, db_index=True)
    ANCHEST = models.CharField('年检结果', max_length=4, null=True, blank=True, choices=cd22_dict)
    ANCHEDATE = models.DateField('年检日期', null=True, blank=True)
    ANCHETYPE = models.CharField('年检类型', max_length=2, null=True, blank=True, choices=cd20_dict)
    ANCHEFORM = models.CharField('年检方式', max_length=2, null=True, blank=True, choices=ex10_dict)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '年检基本信息'
        verbose_name_plural = '年检基本信息'
        app_label = "companyplus"
        db_table = 'company_ac'

    def __unicode__(self):
        return u'%s的年检基础' % self.entid


# 内资年检信息
class ELAnnualCheckIn(EntIdable):
    ANCHEYEAR = models.CharField('年检年度', max_length=4, null=True, blank=True, db_index=True)
    BUSST = models.CharField('营运状况', max_length=4, null=True, blank=True, choices=cd24_dict)
    VENDINC = models.DecimalField('营业收入', max_digits=15, decimal_places=2, null=True, blank=True, help_text='单位：万元')
    SERVINC = models.DecimalField('服务营业收入', max_digits=15, decimal_places=2, null=True, blank=True, help_text='单位：万元')
    PROGRO = models.DecimalField('利润总额', max_digits=15, decimal_places=2, null=True, blank=True, help_text='单位：万元')
    RATGRO = models.DecimalField('纳税总额', max_digits=15, decimal_places=2, null=True, blank=True, help_text='单位：万元')
    NETINC = models.DecimalField('净利润', max_digits=15, decimal_places=2, null=True, blank=True, help_text='单位：万元')
    DEFICIT = models.DecimalField('全年亏损额', max_digits=15, decimal_places=2, null=True, blank=True, help_text='单位：万元')
    ASSGRO = models.DecimalField('资产总额', max_digits=15, decimal_places=2, null=True, blank=True, help_text='单位：万元')
    LTERMINV = models.DecimalField('长期投资', max_digits=15, decimal_places=2, null=True, blank=True, help_text='单位：万元')
    LIAGRO = models.DecimalField('负债总额', max_digits=15, decimal_places=2, null=True, blank=True, help_text='单位：万元')
    LTERMLIAAM = models.DecimalField('长期负债', max_digits=15, decimal_places=2, null=True, blank=True, help_text='单位：万元')
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '内资年检情况'
        verbose_name_plural = '内资年检情况'
        app_label = "companyplus"
        db_table = 'company_ac_in'

    def __unicode__(self):
        return u'%s的内资年检' % self.entid


# 外资年检情况
class ELAnnualCheckFor(EntIdable):
    ANCHEYEAR = models.CharField('年检年度', max_length=4, null=True, blank=True)
    CHEPRO = models.CharField('核对事项', max_length=2, null=True, blank=True, choices=cd21_dict)
    MATSIGN = models.CharField('与登记信息相符标志', max_length=1, null=True, blank=True, choices=ex06_dict)
    ACST = models.TextField('年检时实际情况', null=True, blank=True)
    BUSST = models.CharField('营运状况', max_length=2, null=True, blank=True, choices=cd24_dict)
    PROANDLOSSST = models.CharField('盈亏状况', max_length=2, null=True, blank=True, choices=cd25_dict)
    CURRENCY_id = CrossForeignCharField('币种', max_length=3, null=True, blank=True, db_column='CURRENCY')
    VENDINC = models.FloatField('销售(营业)收入', null=True, blank=True, help_text='万元')
    SERVLNC = models.FloatField('服务营业收入', null=True, blank=True, help_text='万元')
    SALELNDOMEMARAM = models.FloatField('国内销售额', null=True, blank=True, help_text='万元')
    SERVSALGRO = models.FloatField('服务营业额', null=True, blank=True, help_text='万元')
    RATGRO = models.FloatField('纳税总额', null=True, blank=True, help_text='万元')
    CUSDUTY = models.FloatField('关税额', null=True, blank=True, help_text='万元')
    PROGRO = models.FloatField('利润总额', null=True, blank=True, help_text='万元')
    LTERMINV = models.FloatField('长期投资', null=True, blank=True, help_text='万元')
    LIAGRO = models.FloatField('负债总额', null=True, blank=True, help_text='万元')
    LTERMLIAAM = models.FloatField('长期负债', null=True, blank=True, help_text='万元')
    ASSGRO = models.FloatField('资产总额', null=True, blank=True, help_text='万元')
    ASSGRONET = models.FloatField('净资产总额', null=True, blank=True, help_text='万元')
    NETINC = models.FloatField('净利润', null=True, blank=True, help_text='万元')
    VERCAPORG = models.TextField('验资(审计)机构', null=True, blank=True)
    PRADATE = models.DateField('投产开业日期', null=True, blank=True)
    DEFANDUNCPROREA = models.TextField('亏损及非正常生产原因', null=True, blank=True)
    CONTRNAME = models.TextField('承包企业名称', null=True, blank=True)
    CONTRBUSSIGN = models.CharField('承包经营标志', max_length=200, null=True, blank=True)
    EMPNUM = models.FloatField('从业人数', null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '外资年检情况'
        verbose_name_plural = '外资年检情况'
        app_label = "companyplus"
        db_table = 'company_ac_for'

    def __unicode__(self):
        return u'%s的外资年检' % self.entid

    def _currency(self):
        the_currency = list(ELCA04.objects.filter(code=self.CURRENCY_id).values_list("name", flat=True))
        if len(the_currency):
            return the_currency[0]
        return ""

    _currency.short_description = "币种"

    CURRENCY = property(_currency)


##############################################
################### 动产抵押信息 ################
##############################################

# 抵押登记信息
class ELMortReg(EntIdable):
    MORREG_ID = models.CharField('抵押登记ID', max_length=50, null=True, unique=True)
    MORREGCNO = models.CharField('登记证号', max_length=50, null=True, blank=True)
    REGORG_id = CrossForeignCharField('登记机关', db_column='REGORG', max_length=8, null=True, blank=True)
    MORTYPE = models.CharField('状态标识', max_length=1, null=True, blank=True, choices=(('1', '首次抵押'), ('2', '变更、续约'), ('3', '注销'),))
    REGIDATE = models.DateField('登记日期/核准日期', null=True, blank=True)
    MORCANREA = models.CharField('抵押注销原因', max_length=8, null=True, blank=True, choices=cd60_dict)
    CANDATE = models.DateField('抵押注销日期', null=True, blank=True)
    MORTGAGOR = models.CharField('相关企业', null=True, blank=True, max_length=150)
    MORT_DOCTYPE = models.CharField('合同类型', max_length=40, null=True, blank=True)
    MORT_DOCID = models.CharField('合同ID', max_length=40, null=True, blank=True)
    MORT_LOC = models.TextField('地址', null=True, blank=True)
    MORE_TYPE = models.CharField('抵押类型', max_length=4, null=True, blank=True, choices=(('01', '首次抵押'), ('02', '变更、续约'), ('03', '注销'),))
    ENTTYPE_id = CrossForeignCharField('企业(机构)类型', db_column='ENTTYPE', max_length=4, null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '抵押登记信息'
        verbose_name_plural = '抵押登记信息'
        app_label = "companyplus"
        db_table = 'company_mort'

    def __unicode__(self):
        return u'%s-抵押登记-%s' % (self.entid, self.id)

    def _enttype(self):
        the_enttype = list(ELCA16.objects.filter(code=self.ENTTYPE_id).values_list("name", flat=True))
        if len(the_enttype):
            return the_enttype[0]
        return ""

    _enttype.short_description = "企业(机构)类型"

    ENTTYPE = property(_enttype)

    def _regorg(self):
        the_regorg = list(ELCA11.objects.filter(code=self.REGORG_id).values_list("name", flat=True))
        if len(the_regorg):
            return the_regorg[0]
        return ""

    _regorg.short_description = "登记机关"

    REGORG = property(_regorg)


# 动产抵押变更信息
class ELMortAlter(ELBaseModel):
    MORREG_ID = models.ForeignKey('companyplus.ELMortReg', verbose_name='抵押登记ID', db_column='MORREG_ID',
                                  related_name='mort_alter', to_field='MORREG_ID', null=True)
    ALTITEM_ID = models.CharField('变更事项ID', max_length=50, null=True, blank=True)
    ALTCONITEM = models.CharField('变更事项', max_length=4, null=True, blank=True, choices=cd61_dict)
    ALT = models.TextField('变更内容', null=True, blank=True)
    ALTDATE = models.DateField('变更日期', null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '抵押变更信息'
        verbose_name_plural = '抵押变更信息'
        app_label = "companyplus"
        db_table = 'company_mort_alter'

    def __unicode__(self):
        return u'动产抵押变更%s' % self.id


# 动产抵押物信息
class ELMortGua(ELBaseModel):
    MORREG_ID = models.ForeignKey('companyplus.ELMortReg', verbose_name='抵押登记ID', db_column='MORREG_ID',
                                  related_name='mort_gua', to_field='MORREG_ID', null=True)
    GUA_ID = models.CharField('抵押序号', null=True, blank=True, max_length=50)
    GUANAME = models.CharField('抵押物名称', null=True, blank=True, max_length=100)
    OWN = models.CharField('所有权属', null=True, blank=True, max_length=100)
    REMARK = models.TextField('备注', null=True, blank=True)
    GUADES = models.TextField('抵押物描述', null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '抵押物信息'
        verbose_name_plural = '抵押物信息'
        app_label = "companyplus"
        db_table = 'company_mort_gua'

    def __unicode__(self):
        return u'抵押物-%s' % self.id


# 抵押权人、抵押人信息
class ELMortPerson(EntIdable):
    MORREG_ID = models.ForeignKey('companyplus.ELMortReg', verbose_name='抵押登记ID', db_column='MORREG_ID',
                                  related_name='mort_person', to_field='MORREG_ID', null=True)
    personid = models.IntegerField('抵押权人个人ID', null=True, blank=True, default=0)
    MORE_ID = models.CharField('抵押ID', max_length=50)
    MORE = models.CharField('抵押权人', null=True, blank=True, max_length=150)
    BLICNO = models.CharField('证件号', max_length=26, null=True, blank=True)
    BLICTYPE = models.CharField('证件类型', max_length=2, null=True, blank=True)
    MOR_LOC = models.TextField('地址', null=True, blank=True)
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '抵押人/权人'
        verbose_name_plural = '抵押人/权人'
        app_label = "companyplus"
        db_table = 'company_mort_person'

    def __unicode__(self):
        return u'抵押人信息-%s' % self.id


# 担保范围信息
class ELMortClaim(ELBaseModel):
    MORREG_ID = models.ForeignKey('companyplus.ELMortReg', verbose_name='抵押登记ID', db_column='MORREG_ID',
                                  related_name='mort_claim', to_field='MORREG_ID', null=True)
    PRICLASECKIND = models.CharField('被担保主债权种类', max_length=2, null=True, blank=True)
    PRICLASECAM = models.FloatField('被担保主债权数额', null=True, blank=True)
    WARCOV = models.TextField('债权覆盖范围', null=True, blank=True)
    PEFPERFORM = models.DateField('履约起始日期', null=True, blank=True)
    PEFPERTO = models.DateField('履约截止日期', null=True, blank=True)
    REMARK = models.TextField('备注', null=True, blank=True)
    REGCAPCUR_id = CrossForeignCharField('注册资本(金)币种', max_length=3, null=True, blank=True, db_column='REGCAPCUR')
    updated = models.DateTimeField('更新时间', null=True, blank=True)

    class Meta:
        verbose_name = '抵押担保信息'
        verbose_name_plural = '抵押担保信息'
        app_label = "companyplus"
        db_table = 'company_mort_claim'

    def __unicode__(self):
        return u'担保范围-%s' % self.id

    def _regcapcur(self):
        the_regcapcur = list(ELCA04.objects.filter(code=self.REGCAPCUR_id).values_list("name", flat=True))
        if len(the_regcapcur):
            return the_regcapcur[0]
        return ""

    _regcapcur.short_description = "注册资本(金)币种"

    REGCAPCUR = property(_regcapcur)


models_charge_imported = 0  # 仅供主model引用的虚变量
